package org.medicalinstrument.action;

import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.medicalinstrument.bean.*;
import org.medicalinstrument.dao.RentCartDao;
import org.medicalinstrument.dao.RentOrderDetailDao;
import org.medicalinstrument.impl.RentCartDaoImpl;
import org.medicalinstrument.impl.RentOrderDetailDaoImpl;
import org.medicalinstrument.service.RentCartService;
import org.medicalinstrument.service.RentOrderService;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: kongHuan
 * @Date: 2019/3/20 13:41
 * @Description:
 */
public class rentOrderAction extends ActionSupport {

    private String proStr;
    private String custStr;

    private String accountNo;
    private String productQR;
    private String rentOrderId;
    private String rentTime;
    private Float rent;
    private Integer realRentDays;
    private String custPhone;
    private String orderState;
    private String rentTimeLeft;
    private String rentTimeRight;

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }

    public String getOrderState() {
        return orderState;
    }

    public void setOrderState(String orderState) {
        this.orderState = orderState;
    }

    public String getRentTimeLeft() {
        return rentTimeLeft;
    }

    public void setRentTimeLeft(String rentTimeLeft) {
        this.rentTimeLeft = rentTimeLeft;
    }

    public String getRentTimeRight() {
        return rentTimeRight;
    }

    public void setRentTimeRight(String rentTimeRight) {
        this.rentTimeRight = rentTimeRight;
    }

    public RentOrderService getService() {
        return service;
    }

    public void setService(RentOrderService service) {
        this.service = service;
    }

    private Float totalMoney;

    private String returnString;

    RentOrderService service = new RentOrderService();


    public String getProStr() {
        return proStr;
    }

    public void setProStr(String proStr) {
        this.proStr = proStr;
    }

    public String getCustStr() {
        return custStr;
    }

    public void setCustStr(String custStr) {
        this.custStr = custStr;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public Float getTotalMoney() {
        return totalMoney;
    }

    public void setTotalMoney(Float totalMoney) {
        this.totalMoney = totalMoney;
    }

    public Float getRent() {
        return rent;
    }

    public void setRent(Float rent) {
        this.rent = rent;
    }

    public Integer getRealRentDays() {
        return realRentDays;
    }

    public void setRealRentDays(Integer realRentDays) {
        this.realRentDays = realRentDays;
    }

    public String getRentTime() {
        return rentTime;
    }

    public void setRentTime(String rentTime) {
        this.rentTime = rentTime;
    }

    public String getProductQR() {
        return productQR;
    }

    public void setProductQR(String productQR) {
        this.productQR = productQR;
    }

    public String getRentOrderId() {
        return rentOrderId;
    }

    public void setRentOrderId(String rentOrderId) {
        this.rentOrderId = rentOrderId;
    }

    public String getReturnString() {
        return returnString;
    }

    public void setReturnString(String returnString) {
        this.returnString = returnString;
    }

    public String deleteItem(){

        System.out.println(accountNo+"-----");
        return SUCCESS;
    }

    public String submitOrder(){
        Customer customer;
        List<ProductVo> products;
        List<RentOrderDetail> orderDetails = new ArrayList<>();
        JSONObject jsonObject1=JSONObject.fromObject(custStr);
        customer = (Customer) JSONObject.toBean(jsonObject1, Customer.class);

        products = (List<ProductVo>) JSONArray.toCollection(JSONArray.fromObject(proStr),ProductVo.class);

        //订单号，账号+时间戳生成
        String orderId = customer.getAccountNo() + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        RentOrderMaster order = new RentOrderMaster();
        order.setRentOrderId(orderId);
        order.setAccountNo(customer.getAccountNo());
        order.setConsigeeAddr(customer.getConsigeeAddr());
        order.setGetOrConsigee("1");
        order.setOrderState("0");
        order.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        order.setRentTotalMoney(totalMoney);
        //订单项List
        for(ProductVo product : products){
            RentOrderDetail orderDetail = new RentOrderDetail();
            orderDetail.setAccountNo(customer.getAccountNo());
            orderDetail.setCustPhone(customer.getCustPhone());
            orderDetail.setProductQR(product.getProductQR());
            orderDetail.setRentOrderId(orderId);
            orderDetail.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            orderDetail.setRentDays(product.getRentDays());
            orderDetail.setRentMoney(product.getPrice());
            orderDetail.setRentTime(product.getRentTime());
            orderDetails.add(orderDetail);
        }

        //System.out.println("order----"+orderDetails);
        //System.out.println("produs----"+products);


        returnString = service.submitOrder(order,orderDetails);
        //如果下单成功从购物车中移除该项
        RentCartDao dao = new RentCartDaoImpl();
        RentOrderDetailDao detailDao = new RentOrderDetailDaoImpl();
        if("insert_success".equals(returnString)){
            String time;
            for(RentOrderDetail detail : orderDetails){
                time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                try {
                    detail.setRentMoneyTime(time);
                    detailDao.updateRentMoneyTime(detail);
                    dao.deleteRecored(detail.getAccountNo(),detail.getProductQR());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return SUCCESS;
    }

    /**
     * 查询所有订单信息
     * @return
     */
    public String getAll() {

        returnString = service.getAll();
        System.out.println(returnString);
        return SUCCESS;
    }
    /**
     * @Author: kongHuan
     * @Date: 2019/3/28 21:18
     * @Description:获取某个用户的所有订单信息
     */
    public String showOrder(){
        returnString = service.showRentOrder(accountNo);
        return SUCCESS;
    }

    /**
     * @Author: kongHuan
     * @Date: 2019/4/17 8:51
     * @Description:申请归还商品
     */

    public String supplyReturnProduct(){
        RentOrderDetail detail = new RentOrderDetail();
        detail.setProductQR(productQR);
        detail.setAccountNo(accountNo);
        detail.setRentOrderId(rentOrderId);
        returnString = service.supplyReturnProduct(detail);
        return SUCCESS;
    }

    /**
     * @Author: kongHuan
     * @Date: 2019/3/30 10:39
     * @Description:归还商品
     */

    public String returnProduct(){
        RentOrderDetail detail = new RentOrderDetail();
        detail.setProductQR(productQR);
        detail.setAccountNo(accountNo);
        detail.setRentOrderId(rentOrderId);
        detail.setRentTime(rentTime);
        detail.setRent(rent);
        detail.setRealRentDays(realRentDays);
        detail.setReturnTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        System.out.println("action------"+detail);
        returnString = service.returnProduct(detail);
        return SUCCESS;
    }

    //后台管理员查询商品详情
    public String showOrderDetail() {
        //System.out.println(rentOrderId);
        returnString = service.showRentOrderDetail(rentOrderId);
        return SUCCESS;
    }

    //后台管理员条件查询
    public String searchOrderDetail() {
        SearchOption searchOption = new SearchOption();
        searchOption.setRentOrderId(rentOrderId);
        searchOption.setAccountNo(accountNo);
        searchOption.setCustPhone(custPhone);
        searchOption.setOrderState(orderState);
        searchOption.setRentTimeLeft(rentTimeLeft);
        searchOption.setRentTimeRight(rentTimeRight);
       // System.out.println(searchOption);
        returnString = service.searchRentOrderDetail(searchOption);
        return SUCCESS;
    }

    /**
    * @Author: Zing
    * @Date: 2019/4/19 20:58
    * @Description: 订单申请确认管理
    *
    */
    public String confirmApply() {
        RentOrderDetail detail = new RentOrderDetail();
        detail.setProductQR(productQR);
        detail.setAccountNo(accountNo);
        detail.setRentOrderId(rentOrderId);
        detail.setRent(rent);
        detail.setRealRentDays(realRentDays);
        detail.setReturnTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        returnString = service.confirmApply(detail);
        return SUCCESS;
    }

    /**
    * @Author: Zing
    * @Date: 2019/4/22 15:24
    * @Description: 确认客户的商品归还
    *
    */
    public String confirmReturn() {
        RentOrderDetail detail = new RentOrderDetail();
        detail.setAccountNo(accountNo);
        detail.setRentOrderId(rentOrderId);
        detail.setProductQR(productQR);
        returnString = service.confirmReturn(detail);
        return SUCCESS;
    }
}
